home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Enigma Amiga CD / Listati / 64-Maggio-Listato1.c < prev    next >
C/C++ Source or Header  |  1995-04-08  |  8KB  |  219 lines

  1. /****************************************************************************
  2. * Listato 1                                                                 *
  3. * Apre una finestra sul WorkBench di tipo SuperBitMap e GimmeZeroZero; la   *
  4. * bitmap della finestra è 1024x1024, e si disegnano una serie di linee.     *
  5. * Il programma attende che l'utente prema uno dei tasti cursore per sopstare*
  6. * la bitmap; controlla i limiti nel caso venga cambiata la grandezza della  *
  7. * finestra; controlla il gadget di chiusura per l'uscita dal programma.     *
  8. ****************************************************************************/
  9.  
  10. #define INTUI_V36_NAMES_ONLY
  11.  
  12. /* inclusione file di supporto */
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15. #include <intuition/intuition.h>
  16. #include <intuition/screens.h>
  17. #include <graphics/gfx.h>
  18. #include <clib/exec_protos.h>
  19. #include <clib/intuition_protos.h>
  20. #include <clib/graphics_protos.h>
  21. #include <clib/layers_protos.h>
  22. #include <clib/dos_protos.h>
  23.  
  24. struct Library *IntuitionBase = NULL;     /* puntatore a intuition.library */
  25. struct Library *GfxBase = NULL;           /* puntatore a graphics.library */
  26. struct Library *LayersBase = NULL;        /* puntatore a layers.library */
  27. struct Screen *pubscreen = NULL;          /* puntatore al workbench */
  28. struct Window *finestra = NULL;           /* puntatore alla finestra */
  29. struct BitMap *supBitMap = NULL;          /* puntatore alla bitmap */
  30. struct MsgPort *UPort;                    /* puntatore alla porta IDCMP */
  31.  
  32. /* definizione prototipi di funzione */
  33. void WaitEvent(struct MsgPort *,struct IntuiMessage *);
  34. void CloseAll(void);
  35. void OpenAll(void);
  36. void DisegnaTutto(void);
  37. UWORD RangeRand(unsigned long MaxValue); /* funzione di Amiga.lib per la generazione di numeri casuali */
  38.  
  39. void WaitEvent(struct MsgPort *porta,struct IntuiMessage *mess)
  40. {
  41.   struct IntuiMessage *msg;
  42.  
  43.   while ((msg = (struct IntuiMessage *)GetMsg(porta)) == NULL)
  44.     WaitPort(porta);
  45.  
  46.   CopyMem(msg,mess,sizeof(struct IntuiMessage));
  47.   return;
  48. }
  49.  
  50. /* procedura CloseAll() */
  51. void CloseAll()
  52. {
  53.   register ULONG i;
  54.  
  55.   if (finestra != NULL) CloseWindow(finestra);
  56.   for (i=0; i<pubscreen->BitMap.Depth; i++)
  57.     if (supBitMap->Planes[i] != NULL) FreeRaster(supBitMap->Planes[i],1024,1024);
  58.   if (supBitMap != NULL) FreeMem(supBitMap,sizeof(struct BitMap));
  59.   if (pubscreen != NULL) UnlockPubScreen(NULL,pubscreen);
  60.   if (LayersBase != NULL) CloseLibrary(LayersBase);
  61.   if (GfxBase != NULL) CloseLibrary(GfxBase);
  62.   if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
  63.  
  64.   exit(0);
  65. }
  66.  
  67. /* procedura OpenAll() */
  68. void OpenAll()
  69. {
  70.   register ULONG i;
  71.  
  72.   /* apriamo intuition.library, almeno version 36 */
  73.   if ((IntuitionBase = OpenLibrary("intuition.library",36L)) == NULL)
  74.     CloseAll();
  75.  
  76.   /* apriamo graphics.library, almeno version 36; vengono utilizzate delle
  77.      funzioni per il disegno */
  78.   if ((GfxBase = OpenLibrary("graphics.library",36L)) == NULL)
  79.     CloseAll();
  80.  
  81.   /* apriamo layers.library, almeno version 36; necessita per l'utilizzo di una
  82.      funzione per lo scrolling del layer della finestra */
  83.   if ((LayersBase = OpenLibrary("layers.library",36L)) == NULL)
  84.     CloseAll();
  85.  
  86.   /* blocca lo schermo pubblico di default */
  87.   if ((pubscreen = LockPubScreen(NULL)) == NULL)
  88.     CloseAll();
  89.  
  90.   /* alloca la memoria per la struttura BitMap */
  91.   if ((supBitMap = (struct BitMap *)AllocMem(sizeof(struct BitMap),MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  92.     CloseAll();
  93.   /* inizializza i campi della BitMap */
  94.   InitBitMap(supBitMap,pubscreen->BitMap.Depth,1024,1024);
  95.   /* alloca i piani di bit della BitMap */
  96.   for (i=0; i<pubscreen->BitMap.Depth; i++)
  97.   {
  98.     if ((supBitMap->Planes[i] = AllocRaster(1024,1024)) == NULL)
  99.       CloseAll();
  100.   }
  101.   
  102.   /* apre finestra spostabile, ridimensionabile, SuperBitMap, GimmeZeroZero,
  103.      con il gadget di chiusura */
  104.   if ((finestra = OpenWindowTags(NULL,WA_Left,10,
  105.                                       WA_Top,10,
  106.                                       WA_Width,350,
  107.                                       WA_Height,150,
  108.                                       WA_MaxWidth,1024,
  109.                                       WA_MaxHeight,1024,
  110.                                       WA_Title,"Mostra BitMap - usare i tasti freccia",
  111.                                       WA_DragBar,TRUE,
  112.                                       WA_CloseGadget,TRUE,
  113.                                       WA_DepthGadget,TRUE,
  114.                                       WA_SizeGadget,TRUE,
  115.                                       WA_SizeBRight,TRUE,
  116.                                       WA_SizeBBottom,TRUE,
  117.                                       WA_Flags,WFLG_SUPER_BITMAP|WFLG_GIMMEZEROZERO|WFLG_NOCAREREFRESH,
  118.                                       WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_RAWKEY|IDCMP_NEWSIZE,
  119.                                       WA_PubScreen,pubscreen,
  120.                                       WA_SuperBitMap,supBitMap,
  121.                                       TAG_END)) == NULL)
  122.     CloseAll();
  123.   UPort = finestra -> UserPort;
  124.  
  125.   return;
  126. }
  127.  
  128. /* Questa procedura disegna una serie di linee nella BitMap della finestra */
  129. void DisegnaTutto()
  130. {
  131.   register WORD x1,y1,x2,y2,penna,ncol,dx,dy;
  132.  
  133.   ncol = 1 << pubscreen->BitMap.Depth;
  134.   dx = RangeRand(6)+2;
  135.   dy = RangeRand(6)+2;
  136.   penna = RangeRand(ncol-1)+1;
  137.   SetAPen(finestra->RPort,penna); /* imposta il colore da usare per il disegno */
  138.   x2 = 1023; y1 = 0; y2 = 1023;
  139.   for (x1=0; x1<1204; x1+=dx)
  140.   {
  141.     Move(finestra->RPort,x1,y1);
  142.     Draw(finestra->RPort,x2,y2); /* disegna la linea */
  143.     x2-=dx;
  144.   }
  145.   penna = RangeRand(ncol-1)+1;
  146.   SetAPen(finestra->RPort,penna);
  147.   x1 = 0; x2 = 1023; y2 = 1023;
  148.   for (y1=0; y1<1204; y1+=dy)
  149.   {
  150.     Move(finestra->RPort,x1,y1);
  151.     Draw(finestra->RPort,x2,y2);
  152.     y2-=dy;
  153.   }
  154.   return;
  155. }
  156.  
  157. void main()
  158. {
  159.   register LONG px=0,py=0,pp;
  160.   struct IntuiMessage mm;
  161.  
  162.   OpenAll();  /* apre tutte le strutture dati */
  163.   SetDrMd(finestra->RPort,JAM1); /* cancella la BitMap */
  164.   SetAPen(finestra->RPort,0); RectFill(finestra->RPort,0,0,1023,1023);
  165.   DisegnaTutto(); /* disegna le linee */
  166.   /* sposta il layer nella posizione 0,0 */
  167.   pp = finestra->RPort->Layer->Scroll_X; /* determina la posizione X del layer */
  168.   ScrollLayer(0,finestra->RPort->Layer,-pp,0); /* sposta il layer di -X */
  169.   pp = finestra->RPort->Layer->Scroll_Y; /* stesso per Y */
  170.   ScrollLayer(0,finestra->RPort->Layer,0,-pp);
  171.  
  172.   while(1)
  173.   {
  174.     WaitEvent(UPort,&mm);  /* attende un evento */
  175.     if (mm.Class == IDCMP_CLOSEWINDOW) break; /* se gadget di chiusura esci */
  176.     else if (mm.Class == IDCMP_RAWKEY)
  177.     {
  178.       if (mm.Code == 0x4C) /* tasto freccia su */
  179.       {
  180.         pp = py;
  181.         py -= 16;
  182.         if (py<0) py = 0;
  183.         ScrollLayer(0,finestra->RPort->Layer,0,py-pp);
  184.       }
  185.       else if (mm.Code == 0x4D) /* tasto freccia giu */
  186.       {
  187.         pp = py;
  188.         py += 16;
  189.         if (py>(1024-finestra->GZZHeight)) py = 1024-finestra->GZZHeight;
  190.         ScrollLayer(0,finestra->RPort->Layer,0,py-pp);
  191.       }
  192.       else if (mm.Code == 0x4F) /* tasto freccia sinistra */
  193.       {
  194.         pp = px;
  195.         px -= 16;
  196.         if (px<0) px = 0;
  197.         ScrollLayer(0,finestra->RPort->Layer,px-pp,0);
  198.       }
  199.       else if (mm.Code == 0x4E) /* tasto freccia destra */
  200.       {
  201.         pp = px;
  202.         px += 16;
  203.         if (px>(1024-finestra->GZZWidth)) px = 1024-finestra->GZZWidth;
  204.         ScrollLayer(0,finestra->RPort->Layer,px-pp,0);
  205.       }
  206.     }
  207.     else if (mm.Class == IDCMP_NEWSIZE) /* l'utente ha cambiato dimensione, verificare la corretta posizione */
  208.     {
  209.       pp = px;
  210.       if (px>(1024-finestra->GZZWidth)) px = 1024-finestra->GZZWidth;
  211.       ScrollLayer(0,finestra->RPort->Layer,px-pp,0);
  212.       pp = py;
  213.       if (py>(1024-finestra->GZZHeight)) py = 1024-finestra->GZZHeight;
  214.       ScrollLayer(0,finestra->RPort->Layer,0,py-pp);
  215.     }
  216.   }
  217.   CloseAll();  /* chiudi tutto ed esci */
  218. }
  219.